# -*- coding:utf-8 -*-

import time
import os

WORKSPACE_PATH = os.path.dirname(os.path.abspath(__file__))


def build(project):
    print("python co.py --build %s" % project)
    os.system("python co.py --build %s" % project)


def read_config():
    file_object = open('%s/%s' % (WORKSPACE_PATH, 'autobuild.config'), 'r')
    lines = file_object.readlines()
    print(lines)
    file_object.close()

    for line in lines:
        kv = line.split('=')
        if 'TIME' in kv[0]:
            _time = kv[1]
            task_times = _time.split('|')
        elif 'PROJECT' in kv[0]:
            _project = kv[1]
            task_projects = _project.split('|')
    return task_times, task_projects


while True:
    task_times, task_projects = read_config()
    current_hour = int(time.localtime(time.time()).tm_hour)
    current_mins = int(time.localtime(time.time()).tm_min)

    print('task_times:%s' % task_times)
    print('task_projects:%s' % task_projects)
    for _time in task_times:
        hm = _time.split(':')
        hour = int(hm[0])
        mins = int(hm[1])
        print('%s:%s   %s:%s\n' % (current_hour, current_mins, hour, mins))
        if hour == current_hour and mins == current_mins:
            for project in task_projects:
                build(project)
    time.sleep(60)
